

#import "FAMCameAdjustmentView.h"
#import "FAMCameAdjustmentTableViewCell.h"
@interface FAMCameAdjustmentView()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *FAMTableView;

@end
@implementation FAMCameAdjustmentView
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"FAMCameAdjustmentView" owner:self options:nil];
        self = array.firstObject;
        [self.FAMTableView registerNib:[UINib nibWithNibName:@"FAMCameAdjustmentTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
        self.FAMTableView.rowHeight = 50;
        self.FAMTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        self.FAMTableView.delegate = self;
        self.FAMTableView.dataSource = self;
    }
    return self;
}
- (void)layoutSubviews{
    [super layoutSubviews];
    self.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _FAMFoodModel.foodList.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    FAMCameAdjustmentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.FAMFoodModel = _FAMFoodModel.foodList[indexPath.row];
    WeakSelf(self);
    cell.block = ^(FAMFoodIdentifyModel * _Nonnull model) {
        [self->_FAMFoodModel.foodList removeObjectAtIndex:indexPath.row];
        [weakSelf.FAMTableView reloadData];
    };
    return cell;
}


- (IBAction)FAM_closeButtonClick:(UIButton *)sender {
    [self removeFromSuperview];
}
- (IBAction)FAM_saveButtonClick:(UIButton *)sender {
    if (_FAMFoodModel.foodList.count <= 0) {
        [UIWindow.currentViewController showHint:@"Please add food"];
        return;
    }
    NSMutableArray *tempArray = [NSMutableArray array];
    for (int i = 0; i < _FAMFoodModel.foodList.count; i++) {
        FAMFoodIdentifyModel *model = _FAMFoodModel.foodList[i];
        if ([[NSString nullToString:model.food_name] isEqualToString:@""]) {
            [UIWindow.currentViewController showHint:@"Please add the food name"];
            return;
        }
        if ([[NSString nullToString:model.weight] isEqualToString:@""]) {
            [UIWindow.currentViewController showHint:@"Please add food weight"];
            return;
        }
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        [dic setValue:model.food_name forKey:@"food_name"];
        [dic setValue:model.weight forKey:@"weight"];
        [tempArray addObject:dic];
    }
    WeakSelf(self);
    [UIWindow.currentViewController showHudInKeyWindowWithHint:nil];
    [FAMHttpTool FAM_PostFoodReidentifyWithParameters:[NSMutableDictionary dictionaryWithDictionary:@{@"data":tempArray}] success:^(FAMFoodIdentifyModel * _Nonnull model, NSString * _Nonnull msg) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [UIWindow.currentViewController hideHudKeyWindow];
            [UIWindow.currentViewController performSelector:@selector(FAM_loadData:) withObject:model];
            [weakSelf removeFromSuperview];
        });
    } failure:^(NSString * _Nonnull errorInfo) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [UIWindow.currentViewController hideHudKeyWindow];
            [UIWindow.currentViewController showHint:errorInfo];
        });
    }];
}
- (IBAction)FAM_addFoodButtonClick:(UIButton *)sender {
    FAMFoodIdentifyModel *model = [[FAMFoodIdentifyModel alloc]init];
    [_FAMFoodModel.foodList addObject:model];
    [self.FAMTableView reloadData];
}

- (void)setFAMFoodModel:(FAMFoodIdentifyModel *)FAMFoodModel{
    _FAMFoodModel = FAMFoodModel;
    [self.FAMTableView reloadData];
}

@end
